<?php defined('IN_NWIDC') or exit('Access Invalid!');?>
<div class="page">
  <div class="fixed-bar">
    <div class="item-title">
      <h3>代理商统计</h3>
      <ul class="tab-base">
        <li><a href="JavaScript:void(0);" class="current"><span><?php echo $lang['manage'];?></span></a></li>
      </ul>
    </div>
  </div>
  <div class="fixed-empty"></div>
  <form method="get" action="index.php" name="formSearch" id="formSearch">
    <input type="hidden" name="act" value="proxy_order" />
    <input type="hidden" name="op" value="index" />
    <table class="tb-type1 noborder search">
      <tbody>
        <tr>
          <th><label for="query_start_time">日期</label></th>
          <td><input class="txt date" type="text" value="<?php echo $_GET['query_start_time'];?>" id="query_start_time" name="query_start_time">
            <label for="query_start_time">~</label>
            <input class="txt date" type="text" value="<?php echo $_GET['query_end_time'];?>" id="query_end_time" name="query_end_time"/></td>
         <th>代理商ID</th>
         <td><input class="txt-short" type="text" name="buyer_name" value="<?php echo $_GET['buyer_name'];?>" /></td> 
         <td>
            商品名
            <input type='text' name='goods_name' value='<?php echo $_GET[goods_name]?>' />
         </td>
         <td>
           排序
           <select name="order" id="">
             <option value="">请选择</option>
             <option value="time">时间</option>
             <option value="sprice">商品原价</option>
             <option value="pprice">商品代理价</option>
             <option value="margin">商品提成</option>
           </select>
         </td>
          <td><a href="javascript:void(0);" onclick="$('#formSearch').submit();" id="ncsubmit" class="btn-search " title="<?php echo $lang['nc_query'];?>">&nbsp;</a>
            
          </td>
        </tr>
      </tbody>
    </table>
  </form>
  <script>
    function search_member(t){
      $('[name=buyer_name]').val(t.innerHTML);
      $('#formSearch').submit();
    }
  </script>
  <table class="table tb-type2" id="prompt">
    <tbody>
      <tr class="space odd">
        <th colspan="12"><div class="title"><h5><?php echo $lang['nc_prompts'];?></h5><span class="arrow"></span></div></th>
      </tr>
      <tr>
        <td class='overview'>
      <?php

        if(count($_GET) > 2){
          $m = Model();
          $sql = "select *,og.source_price as sprice,og.goods_price as fprice,og.goods_proxy as pprice,sum(og.goods_num) as gnum,o.store_id as store_id from ra_order as o
                  left join ra_order_goods as og
                    on og.order_id = o.order_id
                  left join ra_goods as g
                    on g.goods_id = og.goods_id
                  left join ra_member as m
                    on m.member_id = o.buyer_id
                  where o.order_state = 40
                ";
          if(!empty($_GET['query_start_time'])){
            $sql .= " and o.finnshed_time >= '" . strtotime($_GET['query_start_time']) . "'";
          }
          if(!empty($_GET['query_end_time'])){
            $sql .= " and o.finnshed_time <= '" . strtotime($_GET['query_end_time']) . "'";
          }
          if(!empty($_GET['goods_name'])){
            $sql .= " and og.goods_name like '%{$_GET['goods_name']}%'";
          }
          if(!empty($_GET['buyer_name'])){
            $sql .= " and m.member_name = '$_GET[buyer_name]'";
          }

          
          $sql .= ' group by o.buyer_id,og.goods_id';

          if(!empty($_GET['order'])){
            if($_GET['order'] == 'time'){
              $sql .= " order by o.finnshed_time desc";
            }
            if($_GET['order'] == 'sprice'){
              $sql .= " order by sprice desc";
            }
            if($_GET['order'] == 'pprice'){
              $sql .= " order by pprice desc";
            }
          }
          $ret = $m->query($sql);
        
      ?>
      <span style='display:block;padding-top:10px;padding-bottom:10px;'>可点击代理商对该代理商进行统计</span>
      <?php
        }else{
      ?>
      请设定统计条件进行统计，输入代理商ID可直接统计该代理的相关信息
      <?php    
        }

        if(!empty($_GET['buyer_name'])){
          $user = Model('member')->where(array('member_name' => $_GET['buyer_name']))->find();
          if($user){
            $proxy = Model()->table('proxy')->where(array("member_id" => $user['member_id']))->find();
            if($proxy){
          $total_amount = preg_replace("/select([\s\S]+)from/","select sum(o.order_amount) as amount from",$sql);
          $total_amount = str_replace("group by o.buyer_id,og.goods_id","group by o.order_id",$total_amount);
          $total_amount = str_replace("left join ra_order_goods as og
                    on og.order_id = o.order_id
                  left join ra_goods as g
                    on g.goods_id = og.goods_id","",$total_amount);
           
          $total_amount = str_replace("and o.buyer_id in (select member_id from ra_proxy)","",$total_amount);
          $total_amount = Model()->query($total_amount);
          $total_amount = $total_amount[0]['amount'];


      ?>
      <style>
        .overview dt{
          display: block;
          line-height: 25px;
          font-size: 14px;
          font-weight: bold;
        }
        .overview dd{
          display: block;
          padding-left: 15px;
          line-height: 15px;
          padding-top: 5px;
          margin-bottom: 10px;
        }
        .overview dd ._proxy{
          width: 100px;
          float: left;
          display: block;
          margin-right: 5px;
          margin-bottom: 5px;
        }
      </style>
      <dl>
        <dt>
          代理商名
        </dt>
        <dd>
          <?php echo $user['member_name']?>
        </dd>
      </dl>

      <dl>
        <dt>总业绩</dt>
        <dd><?php echo $total_amount ? $total_amount : 0?></dd>
      </dl>
      <dl>
        <dt>金牌代理</dt>
        <dd>
          <?php echo $proxy['parent_id'] == 0 ? '是' : '否';?>
        </dd>
      </dl>
      <dl>
        <dt>代理信息</dt>
        <dd><?php echo $proxy['infomation'];?></dd>
      </dl>
      <dl>
        <dt>代理商任务</dt>
        <dd>
          <?php
            $task = Model();
            $tasks_num = $task->table('proxy_task')->field("count(*) as num")->where(array('member_id' => $user['member_id']))->find();
            if($tasks_num['num']){
          ?>
          <table width='80%' id='task_table'>
            <thead>
              <th>
                任务编号
              </th>
              <th>
                起始时间
              </th>
              <th>
                结束时间
              </th>
              <th>
                完成金额/任务金额
              </th>
              <th>
                任务状态
              </th>
              <th>
                操作
              </th>
            </thead>
            <?php
              $task->cls();
              $tasks = $task->table('proxy_task')->where(array('member_id' => $user['member_id']))->select();
              foreach($tasks as $t){
            ?>
            <tr>
            <td>
              <?php echo $t['id']?>
            </td>
            <td>
              <?php echo date('Y-m-d H:i:s',$t['start_time'])?>
            </td>
            <td>
              <?php echo date('Y-m-d H:i:s',$t['end_time'])?>
            </td>
            <td>
              <?php
                // $total_money = 
                $task->cls();
                $total = $task->table("order")->field(array('sum(order_amount) as total'))->where(
                  array(
                    "add_time" => array('between',"{$t['start_time']},{$t['end_time']}"),
                    'buyer_id' => $t['member_id'],
                    'order_state' => 40
                  )
                )->find();
                echo $total = ($total['total'] ? $total['total'] : 0);
              ?>
              /
              <?php
                echo $t['money'];
              ?>
            </td>
            <td>
              <?php 
                $time = time();
                if($time > $t['end_time']){
                  echo '超时';
                }
                elseif($time < $t['start_time']){
                  echo '任务未开始';
                }
                else{
                  echo '进行中';
                }
              ?>
              /
              <?php
              if($total > $t['money']){
                echo '已达到要求';
              }
              else{
                echo '未达到要求';
              }
              ?>

            </td>
            <td>
              <a href="javascript:;" data-id='<?php echo $t['id']?>' class='delete_task'>删除</a>
            </td>
            </tr>
            <?php    
              }
            ?>
            <tfoot>
              <tr></tr>
            </tfoot>
          </table>
          <?php    
            }else{
              echo '无';
            }
          ?>

        </dd>
        <style>
          .task_end_time:hover{
            width: 100px;
          }
        </style>
        <dd>
          开始时间：<input type="text" class='txt date task_start_time' readonly="true" name='task_start_time' >
          <span>&nbsp;&nbsp;&nbsp;</span>
          结束时间：<input type="text" class='txt date task_end_time' readonly="true" name='task_end_time'>
          <span>&nbsp;&nbsp;&nbsp;</span>
          销售限额：<input type='text' class='txt' name='task_money' style='width:80px;'/>元
          <p style='display:block;padding-top:10px;'>
            <a href="javascript:;" class='task_add btn' data-id='<?php echo $user[member_id]?>'>任务指派</a>
          </p>
        </dd>
        <script src='/data/resource/layer/layer.js'></script>
        <script>
        $(function(){
          $('[name=task_start_time],[name=task_end_time]').datepicker();
          $('#task_table tr').show();
          $('#task_table tr').unbind('click');
        });
        $('.task_add').click(function(event) {
          var stime = $('[name=task_start_time]').val();
          var etime = $('[name=task_end_time]').val();
          var money = $('[name=task_money]').val();
          if(stime == ''){
            layer.tips("开始时间不能为空","[name=task_start_time]",{tips:1});
            return;
          }
          if(etime == ''){
            layer.tips("结束时间不能为空","[name=task_end_time]",{tips:1});
            return;
          }
          if(money == ''){
            layer.tips("销售额不能为空","[name=task_money]",{tips:1});
            return;
          }

          $.post('index.php?act=proxy_order&op=add_task',{stime:stime,etime:etime,money:money,mid:$(this).attr('data-id')},function(msg){
            if(msg == '添加成功！'){
              layer.msg(msg,{icon:1},function(){
                location.reload();
              });
            }
            else{
              layer.msg(msg,{icon:2});
            }
          });
        });

        $('.delete_task').click(function(){
          var id = $(this).attr('data-id');
          if(confirm('确定要删除吗?')){
            $.post('index.php?act=proxy_order&op=delete_task',{id:id},function(msg){
              if(msg == '删除成功！'){
              layer.msg(msg,{icon:1},function(){
                location.reload();
              });
            }
            else{
              layer.msg(msg,{icon:2});
            }
          });
          }
        });
        </script>
      </dl>
      <dl>
        <dt>下级代理公式</dt>
        <dd>
          <?php echo $proxy['price_formula'] ? $proxy['price_formula'] : '未设置'?>
        </dd>
        <dd>
          参考：假设 100 元的商品，该代理商的价格为 <?php echo cal_proxy(100,$proxy['member_id'])?> 元
        </dd>
        <dd>
          参考：假设 100 元的商品，该代理商下级代理的价格为 <?php echo cal_proxy(100,$proxy['member_id'],$proxy['price_formula'])?> 元
        </dd>
      </dl>
      <dl>
        <dt>下级代理(共 <?php $c = Model()->table('proxy')->field('count(*) as num')->where(array('parent_id' => $proxy['member_id']))->find(); echo $c['num']?> 个)</dt>
        <dd>
          <?php
            $proxys = Model()->table('proxy')->where(array('parent_id' => $proxy['member_id']))->select();
            foreach($proxys as $p){
              $info = Model('member')->find($p['member_id']);
          ?>

          <a href="javascript:;" class='_proxy' onclick='search_member(this)'><?php echo $info['member_name']?></a>
          <?php    
            }
          ?>
        </dd>
      </dl>

      <?php
          }}
        }
      ?>
      
          

        </td>
      </tr>
    </tbody>
  </table>
  <div style="text-align:right;"></div>
  <table class="table tb-type2 nobdb">
    <thead>
      <tr class="thead">
        <th width='30%'>商品</th>
        <th>时间</th>
        <th>所属订单</th>
        <th>代理商</th>
        <th>单价</th>
        <th class='align-center'>数量</th>
        <th class='align-center'>代理价格</th>
        <th class="align-center">代理商提成</th>
        <th>小计</th>
      </tr>
    </thead>
    <tbody valign="middle">
      <?php

          if($ret){
          foreach($ret as $item){
      ?>
      <tr class="hover">
        <td valign="middle">
          <a href="/shop/index.php?act=goods&goods_id=<?php echo $item[goods_id]?>" target='_blank'>
          <img style='float:left;'src="/data/upload/shop/store/goods/<?php echo $item[store_id]?>/<?php echo $item['goods_image']?>" alt="" width='65' height='65'>
          <span style='float:left;margin-left:10px;width:200px;'><?php echo $item['goods_name']?></span>
          </a>
        </td>
        <th>
          <?php echo date('Y-m-d H:i:s',$item['finnshed_time'])?>
        </th>
        <td>
          <a href="index.php?act=order&op=show_order&order_id=<?php echo $item[order_id]?>">
            <?php echo $item['order_sn'];?>
          </a>
        </td>
        <td>
          <a href="javascript:;" onclick='search_member(this)'>
          <?php
            echo $item['member_name'];
          ?>
          </a>
        </td>
        <td>
          <?php echo $item['sprice']?>
        </td>
        <td align="center">
          <?php echo $item['gnum']?>
        </td>
        <td align="center">
          <?php echo $item['pprice']?>
        </td>
        <td align="center">
          <?php echo $item['margin'] * $item['gnum']?>
        </td>
        <td>
          <?php echo $item['fprice'] * $item['gnum'] ?>
        </td>
      </tr>
      <?php      
          }
        }else{
      ?>


      
      <tr class="no_data">
        <td colspan="15">未查到相关数据，如果初次打开，请设定统计条件</td>
      </tr>
      <?php
        }
      ?>
    </tbody>
    <!-- <tfoot>
      <tr class="tfoot">
        <td colspan="15" id="dataFuncs"><div class="pagination"> <?php echo $output['show_page'];?> </div></td>
      </tr>
    </tfoot> -->
  </table>
</div>
<script type="text/javascript" src="<?php echo RESOURCE_SITE_URL;?>/js/jquery-ui/jquery.ui.js"></script> 
<script type="text/javascript" src="<?php echo RESOURCE_SITE_URL;?>/js/jquery-ui/i18n/zh-CN.js" charset="utf-8"></script>
<link rel="stylesheet" type="text/css" href="<?php echo RESOURCE_SITE_URL;?>/js/jquery-ui/themes/ui-lightness/jquery.ui.css"  />
<script type="text/javascript">
$(function(){
    $('#query_start_time').datepicker({dateFormat: 'yy-mm-dd'});
    $('#query_end_time').datepicker({dateFormat: 'yy-mm-dd'});
    // $('#ncsubmit').click(function(){
    	
    // });
});
</script> 
